package com.amat0urheu.dao;



import com.amat0urheu.pojo.CloudPojo.RaceVm;
import com.amat0urheu.pojo.CloudPojo.VMservice;
import com.amat0urheu.pojo.CloudPojo.VmFlag;
import com.amat0urheu.pojo.race.FlagLog;
import com.amat0urheu.pojo.race.Race;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;


import java.util.Date;
import java.util.List;

@Mapper
public interface RaceMapper extends BaseMapper<Race> {
    @Select("select * from  mc_race  order BY createTime DESC ")
    List<Race> selectAllRace();

    @Select("     SELECT *, (SELECT COUNT(*) FROM mc_race_current_ranking WHERE mc_race.rid = mc_race_current_ranking.rid) AS teamcount   FROM mc_race ")
    List<Race> selectAllRace2();

    @Delete("delete   from mc_race where rid=#{rid}")
    int removeByRid(Integer rid);
    @Select("select *  ,(SELECT COUNT(*) FROM mc_race_current_ranking WHERE mc_race.rid = mc_race_current_ranking.rid) AS teamcount from  mc_race  where rid=#{rid}  ")
    Race getRaceInfoById(Integer rid);

    @Select("select rname from  mc_race  where rid=#{rid} ")
    String getRaceName(Integer rid);
    @Select("select count(*) from  mc_race_flag  where rid=#{rid}  and flag=#{flag}")
    int attackVM(Integer rid, String flag);
    @Insert("insert into mc_race_flag_mark (team,flag,rid) values(#{teamName},#{flag},#{rid})")
    void addFlagMark(String teamName, String flag,Integer rid);
    @Select("select count(*) from mc_race_flag_mark where team=#{teamName} and flag=#{flag} and rid=#{rid}")
    int getFlagMark(String teamName, String flag ,Integer rid);
    @Update("update mc_race_flag set attack=attack+1 where flag= #{flag}")
    void updateAttackCount(String flag);
    @Select("select attack from mc_race_flag where flag=#{flag}")
    int getAttackCount(String flag);
    @Select("select maxScore from mc_race_flag where flag=#{flag}")
    int getFlagScore(String flag);
    @Update("update mc_race_current_ranking set teamScore=teamScore+#{score} where teamName= #{teamName} and rid=#{rid}")
    void addScoreToTeam(String teamName, int score,Integer  rid);
    @Insert("insert into mc_race (rname,ctime,dtime,status,flagscore,reducespeed,vmtime,content,createTime) values(#{race.rname},#{race.ctime},#{race.dtime},#{race.status},#{race.flagscore},#{race.reducespeed},#{race.vmtime},#{race.content},CURRENT_TIMESTAMP)")
    int insertRace(@Param("race")Race race);
    @Insert("INSERT INTO mc_vm (vmID,proxyNum, race,vmName,type,noVNC,createTime,deadTime,status,openstackIP) VALUES (#{victimVm.vmID},#{victimVm.proxyNum},#{victimVm.race},#{victimVm.vmName},#{victimVm.type},#{victimVm.noVNC},#{victimVm.createTime},#{victimVm.deadTime},#{victimVm.status},#{victimVm.openstackIP})")
    void insertRaceVm(@Param("victimVm") RaceVm victimVm);
    @Select("select vmtime from mc_race where rname=#{race}")
    int getVmTime(String race);
    @Select("select vmtime from mc_race where rid=#{rId}")
    int getVmTimeByRid(Integer rId);
    @Select("select rid from  mc_race  where rname=#{race} ")
    String  getRaceId(String race);
    @Select("select flagscore from  mc_race  where rname=#{race} ")
    Integer getRaceFlagScore(String race);
    @Insert("insert into mc_race_flag (rid,vm,flag,maxScore) values (#{raceId},#{vmId},#{flag},#{raceFlagScore})")
    void inserFlag(String  raceId,String vmId,String flag,  Integer raceFlagScore);

//    void getRaceVmsForTeam(String raceName, String teanNameByName);
    @Select("select * from mc_vm  where experiment is null  order by createTime Desc ")
    List<VMservice> getAllRaceVms();

    @Select("select * from mc_race_flag_log ")
    List<FlagLog> getSubmitFlagLogs();
    @Insert("insert into mc_race_flag_log values( null,#{name}, #{flag},#{result},#{date})")
    void updateFlag(String name, String flag, Date date, String result);

    @Select("select * from mc_race")
    List<FlagLog> getTeamRanking(String rid);

    @Delete("delete from mc_race_current_ranking where rid=#{rid}" )
    int removeRankingOfRace(String rid);
    @Select("  SELECT *, (SELECT COUNT(*) FROM mc_race_current_ranking WHERE mc_race.rid = mc_race_current_ranking.rid) AS teamcount   FROM mc_race where ctime <CURRENT_TIMESTAMP  and dtime > CURRENT_TIMESTAMP  order BY createTime DESC")
    List<Race> selectAllRaceforUser();

    @Select("select * from  mc_race   where dtime < CURRENT_TIMESTAMP order BY createTime DESC ")
    List<Race> getEndRaceForUser();
    @Select("select rId from  mc_race   where rname =#{race} ")
    Integer getRaceIdByName(String race);

    @Select("select count(*) from  mc_race   where rname =#{race} ")
    int judgeExitsRace(String rname);


    @Insert("insert into mc_race_current_ranking (teamName,rid) values (#{teamName}, #{rid})")
    int addTeamRanking(String teamName,Integer rid);
@Select("select  count(*) from  mc_race_current_ranking where  teamName=#{teamName} and rid=#{rid}")
    int getIfExitTeam(String teamName,Integer rid);

    @Select("select  Id ,vmName,type,noVNC from  mc_vm where  race=#{race} and (  team=#{teamName} or user =#{name}) ")
    List<VMservice> getRaceVmByTeamName(String teamName,String name,String  race);
    @Select("select clientId   from mc_scene a,mc_race_scene b where b.rid=#{rId} and b.sId=a.Id  ")
    Integer getClientId(Integer rId);
    @Select("select  count(*) from  mc_race_flag where  team=#{teamName} and rid=#{rid} and flag=#{flag}")
    int getFlagOfOurTeam(Integer rid, String teamName ,String flag);
    @Select("select   count(*)  from  mc_vm where  race=#{race} and team=#{teamName} and type not like '%client%' ")
    Integer getRaceVmCountByTeamName(String raceName, String teamName);
    @Update("update mc_vm set team=null  where race=#{race} and team=#{teamName} ")
    void releaseRaceVm(String teamName, String race);
    @Update("update mc_vm set user=null  where race=#{race} and user=#{user} ")
    void releaseRaceVmofUser(String race, String user);

    @Insert("insert into mc_race_mark values(null,#{rid},#{teamName})")
    void inserRaceMark(Integer rid, String teamName);

    @Select("select vmID,openstackIP from mc_vm where  race=#{raceName} and team=#{teamName}  ")
    List<VMservice> getRaceVmvmId(String raceName, String teamName);
    @Delete("delete from mc_race_flag where vm= #{vmID}")
    void deleteFlags(String vmID);
    @Select("select vmID,openstackIP from mc_vm where  race=#{race} and team=#{teamName}  ")
    List<VMservice> getClientVm(String raceName, String name);
    @Select("select count(*) from mc_race_mark where rid=#{rid} and team=#{teamName}")
    Integer getNumRaceMark(Integer rid, String teamName);
    @Delete("delete from mc_vm where vmID=#{vmID}")
    void removeRaceVm(String vmID);
    @Select("select * from  mc_race  where  ${key} like #{value}")
    List<Race> searchRaceForAdmin(String key, String value);
    @Select("select * from mc_vm  where  ${key} like #{value}")
    List<VMservice> searchRaceVmForAdmin(String key, String value);
    @Select("select * from mc_race_flag_log  where  ${key} like #{value}")
    List<FlagLog> searchFlagLog(String key, String value);
    @Select("select  dtime from mc_race where rid=#{rid}")
    Date getDeadTime(Integer rid);
    @Select("SELECT v.vmName,flag ,f.attack FROM mc_vm v JOIN mc_race_flag  f  ON v.vmId = f.vm where f.team=#{team} and rid=#{rid} and CURRENT_TIMESTAMP<=#{deadTime}")
    List<VmFlag> getFlagofTeam(String team, Integer rid, Date deadTime);
    @Select("SELECT v.vmName,v.race,flag ,v.team,f.attack FROM mc_vm v JOIN mc_race_flag  f  ON v.vmId = f.vm ")
    List<VmFlag> getFlagofvM();
    @Select("SELECT v.vmName,v.race,flag ,v.team,f.attack FROM mc_vm v JOIN mc_race_flag  f  ON v.vmId = f.vm where v.${key} like #{value}")
    List<VmFlag> searchFlagofVm(String key, String value);

    @Select("select race from mc_vm  where race is not null and experiment is null group by race ")
    List<String> getAllraces();
    @Select("SELECT COUNT(DISTINCT proxyNum) AS NumOfProxyNum FROM mc_vm WHERE race = #{race} AND deadTime > CURRENT_TIMESTAMP AND type NOT LIKE '%client%'")
    int getVmSumofRace(String race);
    @Select("SELECT COUNT(DISTINCT proxyNum) AS NumOfProxyNum FROM mc_vm WHERE race = #{race} AND deadTime > CURRENT_TIMESTAMP AND type NOT LIKE '%client%' and team is not null")
    int getVmUsingofRace(String s);
    @Update("update mc_race set rname=#{race.rname},ctime=#{race.ctime},dtime=#{race.dtime},vmtime=#{race.vmtime},flagscore=#{race.flagscore},content=#{race.content},reducespeed=#{race.reducespeed} where rid=#{race.rid}")
    int updateRace(@Param("race")Race race);
    @Select("select reducespeed from mc_race  where rid =#{rid} ")
    int getReduceSpeedById(Integer rid);

    @Select("select vmID,openstackIP from mc_vm where race  is not null ")
    List<VMservice> findAllRaceVms();
}